package net.edu_soft.ec.asset.controller;


import com.alibaba.fastjson.JSONObject;
import net.edu_soft.ec.asset.pojo.Audit;
import net.edu_soft.ec.asset.pojo.UserInfo;
import net.edu_soft.ec.asset.service.AuditService;
import net.edu_soft.ec.common.util.PuingUtil;
import net.edu_soft.ec.common.util.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.net.www.ParseUtil;

import java.util.List;

/**
 *  @author: PUING
 *  @Date: 2020/4/21 23:01
 *  @Description: 审核控制类
 */
@RestController
@RequestMapping(value = "/audit")
public class AuditController {

    @Autowired
    private AuditService auditService;

    //分类查询审核信息 产品/申请/厂家 返回阐述就是名称加id

    /***
     * 分页查询审核信息
     * @param type 审核类型 0产品/1厂家/2申请
     * @param auditStatus 审核状态  已审核 0 审核中为 1 未审核为 2
     * @param pageNum 当前页
     * @param pageSize 每页数
     * @return
     */
    @RequestMapping(value = "/findALL")
    public ResultVo findALL(Integer type,Integer auditStatus,Integer pageNum,Integer pageSize){
        return ResultVo.succeed("",auditService.findALL(type,auditStatus,pageNum,pageSize));
    }

    /***
     * 审核操作 产品/厂家/申请
     * @param auditId 审核id
     * @param subjectId 主体id 产品/厂家/申请id
     * @param auditType 审核类型 0产品/1厂家/2申请
     * @param auditStatus  审核状态  已审核 0 审核中为 1 未审核为 2
     * @return
     */
    @RequestMapping(value = "/auditOperation")
    public ResultVo auditOperation(Integer auditId,Integer subjectId,Integer auditType,Integer auditStatus){
        //从session中获取
        Session session = SecurityUtils.getSubject().getSession();
        UserInfo u = (UserInfo) session.getAttribute("userInfo");
        Integer userInfoId = u.getUserInfoId();
        String username = u.getUsername();
        //此处应判断session是否存在用户信息
        Audit audit = new Audit();
        audit.setType(auditType);
        audit.setAuditId(auditId);
        audit.setSubjectId(subjectId);
        audit.setUserInfoId(userInfoId);
        audit.setAuditor(username);
        audit.setAuditStatus(auditStatus);
        String m = auditService.auditOperation(audit)==1?"操作成功":"操作失败";
        return ResultVo.succeed(m,null);
    }

    /***
     * 批量审核
     * @param audits 审核信息集合，以json格式接收
     * @return
     */
    @RequestMapping(value = "/auditList")
    public ResultVo auditALLOperation(@RequestBody String audits){
        //从session中获取
        Session session = SecurityUtils.getSubject().getSession();
        UserInfo u = (UserInfo) session.getAttribute("userInfo");
        Integer userInfoId = u.getUserInfoId();
        String username = u.getUsername();
        //此处应判断session是否存在用户信息
        PuingUtil.isEmpty("审核：管理员名字为空",username);

        String m = auditService.auditALLOperation(audits)==1?"操作成功":"操作失败";
        return ResultVo.succeed(m,null);
    }
}
